package com.example.demo.config;


import com.example.demo.interceptor.LoginCheckInterceptor;
import com.example.demo.interceptor.RoleCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration//配置类
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;
    
    @Autowired
    private RoleCheckInterceptor roleCheckInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录检查拦截器 - 验证JWT是否有效
        registry.addInterceptor(loginCheckInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/User_login", "/User_Register", "/HR_login", "/HR_Register", "/User_unsafe_login", "/User_unsafe_register");
        
        // 角色权限检查拦截器 - 验证角色权限
        registry.addInterceptor(roleCheckInterceptor)
                .addPathPatterns("/User_", "/HR_", "/api/chat/*", "/user/list", "/select_job_page", "/add_job", "/delete_job", "/update_job", "/get_job_by_id")
                .excludePathPatterns("/User_login", "/User_Register", "/HR_login", "/HR_Register", "/User_unsafe_login", "/User_unsafe_register");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("http://*", "https://*") // 使用模式匹配
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600); // 预检请求缓存时间
    }
}
